import { request } from '@/utils/request';

/**
 * 获取用户列表
 * @param params
 * @returns
 */
export async function getUserList(params?: {
    keyword?: string;
    type?: string;
    follow_time?: string;
}) {
    const res = await request({
        url: `/marketapi/index/getConversationList`,
        params,
        method: 'POST',
        data: {
            //MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAtBHOIi7nYVoZfRqGu2L
            // test123456
            key: 'test123456',
        },
    });

    if (res.status) {
        const data = res.data as Record<string, any>;

        if (data['list'] && Array.isArray(data['list'])) {
            return data['list'] as Record<string, any>[];
        }
    }

    return [];
}

/**
 * 获取与用户对话的聊天内容
 * @param params
 * @returns
 */
export async function getMessage(params: Record<string, any>) {
    const res = await request({
        url: `/marketapi/index/getMessageChatLog`,
        params,
    });

    if (res.status) {
        const data = res.data as Record<string, any>;
        if (data['list'] && Array.isArray(data['list'])) {
            return data['list'] as Record<string, any>[];
        }
    }
    return [];
}

/**
 * 设置用户备注
 * @param data
 * @returns
 */
export async function setUserRemark(data: Record<string, any>) {
    const res = await request({
        url: `/marketapi/index/setUserConversationRemarks`,
        method: 'POST',
        data,
    });

    if (res.status) {
        const data = res.data as Record<string, any>;
        return data;
    }

    return {};
}

/**
 *  获取话术方法
 * @returns
 */
export async function getJargon() {
    // 创建异步等待的变量 调用了request方法
    const res = await request({
        url: `/marketapi/index/getScriptList`,
    });

    if (res.status) {
        // 将res.data赋值给变量data 且变量的类型是key为string,value是any
        const data = res.data as Record<string, any>;
        // Array.isArray()用于判断是否为是一个数组
        if (data['list'] && Array.isArray(data['list'])) {
            // 是的话就返回 类型是一个数组且每一个元素的key为string,value是any
            return data['list'] as Record<string, any>[];
        }
    }
    // 这里为了上面的if语句 没有返回值 而导致的报错 所以在最后返回一个空数组
    return [];
}

/**
 * 设置用户类型
 * @param data
 * @returns
 */
export async function setUserType(data: {
    conversation_id: string;
    type: number;
}) {
    const res = await request({
        url: `/marketapi/index/setUserConversationType`,
        method: 'POST',
        data,
    });

    if (res.status) {
        const data = res.data as Record<string, any>;
        return data;
    }

    return {};
}

/**
 * 发送消息
 * @param data
 * @returns
 */
export async function sendMessage(data: Record<string, any>) {
    const res = await request({
        url: `/marketapi/index/sendMessage`,
        method: 'POST',
        data,
    });

    return res.status;
}

/**
 * 获取用户评论
 * @param params
 */
export async function getUserComment(params: { to_sec_uid: string }) {
    const res = await request({
        url: '/marketapi/index/getAllUserComments',
        params,
    });

    if (res.status) {
        const data = res.data as Record<string, any>;

        if (data['comments_list'] && Array.isArray(data['comments_list'])) {
            return data['comments_list'] as Record<string, any>[];
        }
    }

    // 没有返回值 调用getUserComment回报错 显示类型为void 无法分配
    return [];
}

/**
 * 跟进更新
 * @param data
 * @returns
 */
export async function upFollowUpDate(data: Record<string, any>) {
    const res = await request({
        url: `/marketapi/index/setUserConversationFollow`,
        method: 'POST',
        data,
    });

    return res.status;
}

/**
 * 点击用户列表红点消失接口
 * @param data
 * @returns
 */
export async function getRedDot(data: { conversation_id: string | number }) {
    const res = await request({
        url: '/marketapi/index/setChatRedDot',
        method: 'POST',
        data,
    });

    return res.status;
}

/**
 * 设置会话用户的需求
 * @param data
 */
export async function setUserNeed(data: {
    conversation_id?: string;
    is_we_chat?: boolean;
}) {
    const res = await request({
        url: '/marketapi/index/setUserNeed',
        method: 'POST',
        data,
    });

    return res.data;
}

/**
 * 获取AI开关
 * @returns
 */
export async function getAiStatusSwitch() {
    const res = await request({
        url: '/marketapi/index/getAiStatusSwitch',
        method: 'GET',
    });

    return res.data as { ai_status: 0 | 1 };
}

/**
 * 修改AI开关
 * @param data
 * @returns
 */
export async function setAiStatusSwitch(data: { ai_status: number }) {
    const res = await request({
        url: '/marketapi/index/setAiStatusSwitch',
        method: 'POST',
        data,
    });

    return res.data;
}

/**
 * 设置单个会话AI开关
 * @param data
 * @returns
 */
export async function setAiStatusSwitchChat(data: {
    conversation_id: string;
    ai: string;
}) {
    const res = await request({
        url: '/marketapi/index/setAiStatusSwitchChat',
        method: 'POST',
        data,
    });

    return res.status;
}
